/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.JTextField;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.view.Console;
import br.ufmg.dees.insane.commons.view.ConsoleHandler;
import br.ufmg.dees.insane.model.ModelState;
import br.ufmg.dees.insane.ui.rich.full.command.AboutCommand;
import br.ufmg.dees.insane.ui.rich.full.command.CloseCommand;
import br.ufmg.dees.insane.ui.rich.full.command.CubicCommand;
import br.ufmg.dees.insane.ui.rich.full.command.DeleteCommand;
import br.ufmg.dees.insane.ui.rich.full.command.DoesNothingCommand;
import br.ufmg.dees.insane.ui.rich.full.command.ExitCommand;
import br.ufmg.dees.insane.ui.rich.full.command.FitCommand;
import br.ufmg.dees.insane.ui.rich.full.command.GridCommand;
import br.ufmg.dees.insane.ui.rich.full.command.HelpTopicsCommand;
import br.ufmg.dees.insane.ui.rich.full.command.LastZoomCommand;
import br.ufmg.dees.insane.ui.rich.full.command.LineCommand;
import br.ufmg.dees.insane.ui.rich.full.command.NewFileCommand;
import br.ufmg.dees.insane.ui.rich.full.command.OpenCommand;
import br.ufmg.dees.insane.ui.rich.full.command.PartAllPrimitCommand;
import br.ufmg.dees.insane.ui.rich.full.command.PartPrimitCommand;
import br.ufmg.dees.insane.ui.rich.full.command.PointCommand;
import br.ufmg.dees.insane.ui.rich.full.command.PolylineCommand;
import br.ufmg.dees.insane.ui.rich.full.command.PrintCommand;
import br.ufmg.dees.insane.ui.rich.full.command.QuadCommand;
import br.ufmg.dees.insane.ui.rich.full.command.RedrawCommand;
import br.ufmg.dees.insane.ui.rich.full.command.SaveFileAsCommand;
import br.ufmg.dees.insane.ui.rich.full.command.SaveFileCommand;
import br.ufmg.dees.insane.ui.rich.full.command.SelectAllCommand;
import br.ufmg.dees.insane.ui.rich.full.command.SnapCommand;
import br.ufmg.dees.insane.ui.rich.full.command.SpLineCommand;
import br.ufmg.dees.insane.ui.rich.full.command.ZoomAllCommand;
import br.ufmg.dees.insane.ui.rich.full.command.ZoomCommand;
import br.ufmg.dees.insane.ui.rich.full.command.ZoomInCommand;
import br.ufmg.dees.insane.ui.rich.full.command.ZoomKeybCommand;
import br.ufmg.dees.insane.ui.rich.full.command.ZoomOutCommand;

/**
* This class is a implementation of the interface ActionListener.
* It is used to deal with events from Console's Prompt
* @author Fonseca,Flavio & Pitangueira, Roque
* @since January 2004
* @see java.awt.event.ActionListener
* @see gui.Console
*/
public class InsaneConsoleHandler extends ConsoleHandler {
	
	private InsaneInternalInterface parent;
	
//*****************************************************************************
	
	/** Constructor Method.
	* @param parent The parent Interface.
	*/
	public InsaneConsoleHandler(InsaneInternalInterface parent) {
		this.parent = parent;
	}
	
//*****************************************************************************
	
	/** Method which executes the desired action to each valid command.*/
	public void actionPerformed(ActionEvent evt) {
		JTextField source = (JTextField)evt.getSource();
		
		//Works only if prompt is not empty
		if (!evt.getActionCommand().equals("")) {
			//Writes command to output area and clears Prompt
			parent.getConsole().addOutputText("\"" + evt.getActionCommand() + "\"");
			parent.getConsole().addToHistory(evt.getActionCommand());
			counter = -1;
			
			source.setText("");
			
			//Converts the command to up case and stores it into str
			str = evt.getActionCommand().toUpperCase();
			
			if (str.equals("CLEAR")) {parent.getConsole().clear(); cmd = new DoesNothingCommand(parent);}
			else if (str.equals("REDRAW")) {cmd = new RedrawCommand(parent);}
			
			//Commands for changing treeNode
			else if (str.equals("SHAPES")) {parent.setToShapes(); cmd = new DoesNothingCommand(parent); setToCommandMode();}
			else if (str.equals("PARTITION")) {parent.setToPartition(); cmd = new DoesNothingCommand(parent); setToCommandMode();}
			else if (str.equals("NODAL ATTRIBUTES") || str.equals("NODALATTRIBUTES")) {parent.setToNodalAttributes(); cmd = new DoesNothingCommand(parent); setToCommandMode();}
			else if (str.equals("ELM ATTRIBUTES") || str.equals("ELMATTRIBUTES") || str.equals("ELEMENTATTRIBUTES") || str.equals("ELEMENT ATTRIBUTES"))
				{parent.setToElmAttributes(); cmd = new DoesNothingCommand(parent); setToCommandMode();}
			
			//Commands for StateButtons
			else if (str.equals("GRID")) {cmd = new GridCommand(parent);}
			else if (str.equals("SNAP")) {cmd = new SnapCommand(parent);}
			
			//Commands for File
			else if (str.equals("EXIT")) {cmd = new ExitCommand(parent);}
			else if (str.equals("NEW")) {cmd = new NewFileCommand(parent);}
			else if (str.equals("OPEN")) {cmd = new OpenCommand(parent);}
			else if (str.equals("SAVE")) {cmd = new SaveFileCommand(parent);}
			else if (str.equals("SAVEAS") || str.equals("SAVE AS")) {cmd = new SaveFileAsCommand(parent);}
			else if (str.equals("CLOSE")) {cmd = new CloseCommand(parent);}
			else if (str.equals("PRINT")) {cmd = new PrintCommand(parent);}
			
			//Commands for View
			else if (str.equals("ZOOM")) {
				cmd = new ZoomCommand(parent);
				setKeybCommand(new ZoomKeybCommand(parent));
				setToDataMode();
				parent.addOutputText("Click first corner of window or");
				parent.addOutputText("All / Extents / In / Last / Out");
			}
			else if (str.equals("ZOOMWINDOW") || str.equals("ZOOM WINDOW")) {cmd = new ZoomCommand(parent);}
			else if (str.equals("ZOOMALL") || str.equals("ZOOM ALL")) {cmd = new ZoomAllCommand(parent);}
			else if (str.equals("ZOOMEXTENTS") || str.equals("ZOOM EXTENTS")) {cmd = new FitCommand(parent);}
			else if (str.equals("ZOOMIN") || str.equals("ZOOM IN")) {cmd = new ZoomInCommand(parent);}
			else if (str.equals("ZOOMOUT") || str.equals("ZOOM OUT")) {cmd = new ZoomOutCommand(parent);}
			else if (str.equals("ZOOMLAST") || str.equals("ZOOM LAST")) {cmd = new LastZoomCommand(parent);}
			
			//Commands for Edit
			else if (str.equals("UNDO")) {cmd = parent.getUndoCommand();}
			else if (str.equals("REDO")) {cmd = parent.getRedoCommand();}
			else if (str.equals("SELECTALL") || str.equals("SELECT ALL")) {cmd = new SelectAllCommand(parent);}
			else if (str.equals("DELETE") || str.equals("DEL")) {cmd = new DeleteCommand(parent);}
			
			//Commands for Help
			else if (str.equals("HELP")) {cmd = new HelpTopicsCommand(parent.getOwner());}
			else if (str.equals("ABOUT")) {cmd = new AboutCommand(parent.getOwner());}
			
			//Commands for Draw (works only if actualTreeNode == ModelState.SHAPES)
			else if (parent.getModelState().getActualTreeNode() == ModelState.SHAPES) {
				if (str.equals("POINT")) {cmd = new PointCommand(parent);}
				else if (str.equals("LINE")) {cmd = new LineCommand(parent);}
				else if (str.equals("POLYLINE") || str.equals("POLY")) {cmd = new PolylineCommand(parent);}
				else if (str.equals("QUADRATIC") || str.equals("QUAD")) {cmd = new QuadCommand(parent);}
				else if (str.equals("CUBIC")) {cmd = new CubicCommand(parent);}
				else if (str.equals("SPLINE")) {cmd = new SpLineCommand(parent);}
			}
			
			//Commands for Mesh (works only if actualTreeNode == ModelState.PARTITION)
			else if (parent.getModelState().getActualTreeNode() == ModelState.PARTITION) {
				if (str.equals("PART")) {cmd = new PartPrimitCommand(parent); setToCommandMode();}
				else if (str.equals("PARTALL") || str.equals("PART ALL") || str.equals("PARTITIONALL") || str.equals("PARTITION ALL"))
					{cmd = new PartAllPrimitCommand(parent); setToCommandMode();}
			}
			
			
			
			
			//Insert new commands over here
			
			//for COMMAND_MODE
			if (mode == COMMAND_MODE) {
				try { 
					cmd.execute();
					cmd = null;
				}catch (Exception e) {
					parent.getConsole().addOutputText("Invalid Command.");
				}
			}
			
			//for DATA_MODE
			else {
				try {
					cmd.execute();
					cmd = null;
				}catch (Exception e) {
					cmd2.execute();
				}
			}
		}
	}
	
//*****************************************************************************
	
	/** Method to treat events from a key.
	*@param evt The key event to be treated;
	*/
	public void keyReleased(KeyEvent evt) {
		Console cons = parent.getConsole();
		if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
			if (counter > 0) {counter--;}
			str = (String) cons.getHistory().get(counter);
			cons.setPromptText(str);
		} else if (evt.getKeyCode() == KeyEvent.VK_UP) {
			if (counter < cons.getHistory().size()-1) {counter++;}
			str = (String) cons.getHistory().get(counter);
			cons.setPromptText(str);
		} else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
			cmd = new RedrawCommand(parent);
			cmd.execute();
		}
	}
	
//*****************************************************************************
	
	/** Sets this ConsoleHandler to data mode.*/
	public void setToDataMode() {
		mode = DATA_MODE;
	}
	
//*****************************************************************************
	
	/** Sets this ConsoleHandler to command mode.*/
	public void setToCommandMode() {
		mode = COMMAND_MODE;
	}
	
//*****************************************************************************
	
	/** Sets the keyboard command (cmd2).
	* @param command The kayboard command to be set.
	*/
	public void setKeybCommand(Command command) {
		cmd2 = command;
	}
	
//*****************************************************************************
	
	/** Returns the keyboard command (cmd2).
	* @return The kayboard command (cmd2).
	*/
	public Command getKeybCommand() {
		return cmd2;
	}
	
//*****************************************************************************
}